/**
 * 修改commonParam中的值
 * @param commonParam
 * @param field
 * @param value
 */
export function commonParamChange(commonParam, field, value) {
    // 遍历commonParam
    for (let i = 0; i < commonParam.length; i++) {
        let commonInfo = commonParam[i]
        if (commonInfo.variableName === field) {
            if (commonInfo.variableType === "String") {
                if (typeof value === "object" || typeof value === "boolean") {
                    commonInfo.variableValue = JSON.stringify(value)
                } else {
                    commonInfo.variableValue = value
                }
            } else {
                if (typeof value === "object" || typeof value === "boolean") {
                    commonInfo.variableValue = value
                } else {
                    // 将单引号转换为双引号
                    let newStr = value.replaceAll("\'", "\"")
                    commonInfo.variableValue = JSON.parse(newStr)
                }
            }
        }
    }
}

/**
 * 获取commonParam中的值
 * @param commonParam
 * @param field
 * @param value
 */
export function getCommonParam(commonParam, field) {
    // 遍历commonParam
    for (let i = 0; i < commonParam.length; i++) {
        let commonInfo = commonParam[i]
        if (commonInfo.variableName === field) {
            return commonInfo.variableValue
        }
    }
    return "";
}

/**
 * 获取commonParam中对应的对象
 * @param commonParam
 * @param field
 * @param value
 */
export function getCommonParamObj(commonParam, field) {
    // 遍历commonParam
    for (let i = 0; i < commonParam.length; i++) {
        let commonInfo = commonParam[i]
        if (commonInfo.variableName === field) {
            return commonInfo
        }
    }
    return "";
}

/**
 * 删除commonParam参数
 * @param commonParam
 * @param field
 */
export function deleteCommonParam(commonParam, field) {
    let index = -1;
    for (let i = 0; i < commonParam.length; i++) {
        let commonInfo = commonParam[i]
        if (commonInfo.variableName === field) {
            index = i;
        }
    }
    if (index !== -1) {
        commonParam.splice(index, 1);
    }
}

/**
 * 获取commonParam中值-根据类型进行转换
 * @param commonParam
 * @param field
 * @param value
 */
export function getCommonParamInfo(commonParam, field) {
    // 遍历commonParam
    for (let i = 0; i < commonParam.length; i++) {
        let commonInfo = commonParam[i]
        if (commonInfo.variableName === field) {
            if (commonInfo.variableType === "String" || commonInfo.variableValue === "") {
                return commonInfo.variableValue
            } else {
                if (typeof commonInfo.variableValue === "object") {
                    return commonInfo.variableValue
                }
                return JSON.parse(commonInfo.variableValue)
            }
        }
    }
    return "";
}

/**
 * 睡眠
 * @param seconds
 * @returns {Promise<unknown>}
 */
export function sleep(seconds) {
    return new Promise(resolve => setTimeout(resolve, seconds * 1000));
}

/**
 * 获取变量中的值
 * @param commonParam
 * @param value
 * @returns {*}
 */
export function execCustomValue(commonParam, value) {
    // 如果值是以this.getCommonParam开头，则执行当前数据
    let matchStr = "this.getCommonParam";
    if (value.startsWith(matchStr)) {
        // 获取要查询的字段值
        let field = value.substring(matchStr.length + 1, value.length - 1);
        return getCommonParam(commonParam, field);
    }
    return value;
}